#' ---
#' title: "Evaluating Escalation"
#' author: "Erik Lin-Greenberg"
#' date: "9/12/2022"
#' output: pdf_document
#' ---
#' 
## -----------------------------------------------------------------------------
rm(list = ls())
setwd("~/...")
library(Rmisc)
library(plotrix)
library(ggplot2)

#'
## -----------------------------------------------------------------------------
Escalation_Data <- read.csv("Expert Escalation.csv") #read in data

#' 
## -----------------------------------------------------------------------------
#Construct a for-loop to find the means and CIs of all the data
#The loop applies the summarySE function to each variable 
#it is necessary to do the first by hand in order to use rbind effectively 
#Rename the third column to "mean" 

# Main Text, Table 2, Full Sample column (Escalation_Data_Table1)
Escalation_Data_Table1 <- summarySE(data = Escalation_Data, measurevar = 1)
colnames(Escalation_Data_Table1)[3] <- "mean"
for(i in 2:8){
  summary <- summarySE(data = Escalation_Data, 
                       measurevar = i)
  colnames(summary)[3] <- "mean"
  Escalation_Data_Table1 <- rbind(Escalation_Data_Table1, summary)
}

colnames(Escalation_Data_Table1)[1] <- "name"
rownames(Escalation_Data_Table1) <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add row names
Escalation_Data_Table1$name <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add name column

Escalation_Data_Table1$name <- factor(Escalation_Data_Table1$name, levels = Escalation_Data_Table1$name[order(Escalation_Data_Table1$mean)])
Escalation_Data_Table1 <- Escalation_Data_Table1[order(Escalation_Data_Table1$mean),] #sorts df by mean

#' 
## -----------------------------------------------------------------------------

# Graphing Full Sample (Main Text, Figure 1)

tiff("Figure1.tif", res=1200, height=10, width=10, units="in")
ggplot(Escalation_Data_Table1, aes(x = name, y = mean)) + geom_point(size=3) + ylim(1,8) + 
  geom_errorbar(aes(ymin = mean - ci, ymax = mean + ci), width = .2) +
  theme_minimal() + xlab("Actions") + ylab("Mean Perceived Escalation") +
  theme(axis.text = element_text(size = 12), axis.title=element_text(size = 14), plot.title = element_text (size=16)) + coord_flip() +
  ggtitle("Figure 1: Mean Escalation Ranking, Full Sample")
dev.off()

#' 
## -----------------------------------------------------------------------------
Escalation_Data_Table1$name

#' 
## -----------------------------------------------------------------------------
# Subset data by country. United States (Country == 1)
# Main Text, Table 2, U.S. Column

US <- subset(Escalation_Data, Escalation_Data$Country==1)

US_Table <- summarySE(data = US, measurevar = 1)
colnames(US_Table)[3] <- "mean"
for(i in 2:8){
  summary <- summarySE(data = US, 
                       measurevar = i)
  colnames(summary)[3] <- "mean"
  US_Table <- rbind(US_Table, summary)
}

colnames(US_Table)[1] <- "name"
rownames(US_Table) <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add row names
US_Table$name <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add name column

# Order table like the previous full-sample one (e.g. not by the US table means)
US_Table$name <- factor(US_Table$name, levels = c("Cyber attack", "Support to proxies", "Drone strike", "SOF raid (on ship)",
                                                 "Special forces (SOF) raid", "Missile strike", 
                                                 "Manned bomber attack", "Ground attack"))
US_Table <- US_Table[order(US_Table$name),] #sorts df by overall mean
US_Table$country <- "US"

#' 
## -----------------------------------------------------------------------------
# Subset data by country. India (Country == 2)
# Main Text, Table 2, India Column
India <- subset(Escalation_Data, Escalation_Data$Country==2)

India_Table <- summarySE(data = India, measurevar = 1)
colnames(India_Table)[3] <- "mean"
for(i in 2:8){
  summary <- summarySE(data = India, 
                       measurevar = i)
  colnames(summary)[3] <- "mean"
  India_Table <- rbind(India_Table, summary)
}

colnames(India_Table)[1] <- "name"
rownames(India_Table) <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add row names
India_Table$name <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add name column

India_Table$name <- factor(India_Table$name, levels = c("Cyber attack", "Support to proxies", "Drone strike", 
                                                        "SOF raid (on ship)", "Special forces (SOF) raid", "Missile strike", 
                                                 "Manned bomber attack", "Ground attack"))
India_Table <- India_Table[order(India_Table$name),] #sorts df by overall mean
India_Table$country <- "India"

#' 
#' 
## -----------------------------------------------------------------------------
# Subset data by country. Singapore (Country == 3)
# Main Text, Table 2, Singapore Column

Singapore <- subset(Escalation_Data, Escalation_Data$Country==3)

Singapore_Table <- summarySE(data = Singapore, measurevar = 1)
colnames(Singapore_Table)[3] <- "mean"
for(i in 2:8){
  summary <- summarySE(data = Singapore, 
                       measurevar = i)
  colnames(summary)[3] <- "mean"
  Singapore_Table <- rbind(Singapore_Table, summary)
}

colnames(Singapore_Table)[1] <- "name"
rownames(Singapore_Table) <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add row names
Singapore_Table$name <- c("Special forces (SOF) raid", "SOF raid (on ship)", "Drone strike", "Manned bomber attack", 
                               "Missile strike", "Cyber attack", "Support to proxies", "Ground attack") #add name column

Singapore_Table$name <- factor(Singapore_Table$name, levels = c("Cyber attack", "Support to proxies", "Drone strike", 
                                                        "SOF raid (on ship)", "Special forces (SOF) raid", "Missile strike", 
                                                 "Manned bomber attack", "Ground attack"))
Singapore_Table <- Singapore_Table[order(Singapore_Table$name),] #sorts df by overall mean
Singapore_Table$country <- "Singapore"

#' 
## -----------------------------------------------------------------------------
# Graphing By Country (Main Text, Figure 2)

Table2 <- rbind(US_Table, India_Table, Singapore_Table) #combine dfs 

pd <- position_dodge(0.5)

tiff("Figure2.tif", res=1200, height=10, width=10, units="in")
ggplot(Table2, aes(x = name, y = mean, color = country)) + ylim(1,8) + 
  geom_point(aes(color=country, shape=country), size=3, position = pd) +
  geom_errorbar(aes(ymin = mean - ci, ymax = mean + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Actions") + ylab("Mean Perceived Escalation") +
  theme(axis.text = element_text(size = 12), axis.title=element_text(size = 14), plot.title = element_text (size=16), legend.title = element_text (size=16)) + coord_flip() +
  ggtitle("Figure 2: Mean Escalation Ranking, By Country")
dev.off()

#' 
##------------------------------------------------------------------------------
# Demographics
# Gender
Gender <- table(Escalation_Data$Gender)
prop.table(Gender)

US_Gender <- table(US$Gender)
prop.table(US_Gender)

India_Gender <- table(India$Gender)
prop.table(India_Gender)

Singapore_Gender <- table(Singapore$Gender)
prop.table(Singapore_Gender)

#Age 
Age <- median(Escalation_Data$Age, na.rm = TRUE)

US_Age <- median(US$Age,na.rm = TRUE)

India_Age <- median(India$Age, na.rm = TRUE)

Singapore_Age <- median(Singapore$Age, na.rm = TRUE)

# Military Veteran
Veteran <- table(Escalation_Data$Veteran)
prop.table(Veteran)

US_Veteran <- table(US$Veteran)
prop.table(US_Veteran)

India_Veteran <- table(India$Veteran)
prop.table(India_Veteran)

Singapore_Veteran <- table(Singapore$Veteran)
prop.table(Singapore_Veteran)

# Current/Past Government Experience 
Government <- table(Escalation_Data$Government)
prop.table(Government)

US_Government <- table(US$Government)
prop.table(US_Government)

India_Government <- table(India$Government)
prop.table(India_Government)

Singapore_Government <- table(Singapore$Government)
prop.table(Singapore_Government)

# Current Affiliation
Affiliation <- table(Escalation_Data$Current_Affiliation)
prop.table(Affiliation)

US_Affiliation <- table(US$Current_Affiliation)
prop.table(US_Affiliation)

India_Affiliation <- table(India$Current_Affiliation)
prop.table(India_Affiliation)

Singapore_Affiliation <- table(Singapore$Current_Affiliation)
prop.table(Singapore_Affiliation)

# Political Ideology
PoliticalID <- table(Escalation_Data$Political_ID)
prop.table(PoliticalID)

US_PoliticalID <- table(US$Political_ID)
prop.table(US_PoliticalID)

India_PoliticalID <- table(India$Political_ID)
prop.table(India_PoliticalID)

Singapore_PoliticalID <- table(Singapore$Political_ID)
prop.table(Singapore_PoliticalID)



## ---- include = FALSE---------------------------------------------------------
library(knitr)
purl("Evaluating Escalation_Replicaition_12Sept22.rmd", output = "Evaluating Escalation_Replication_12Sep22.r", documentation = 2)

